# coding=utf-8

ITEM_TOC_TMPL = """        <item id="toc" media-type="text/x-oeb1-document" href="{HREF}"></item>
"""

ITEM_CONTENT_TMPL = """        <item id="content{N}" media-type="text/x-oeb1-document" href="{HREF}"></item>
"""

ITEM_NCX_TMPL = """        <item id="ncx" media-type="application/x-dtbncx+xml" href="{HREF}"/>
"""

ITEMREF_TMPL = """        <itemref idref="{IDREF}"/>
"""

OPF_TMPL = """<?xml version="1.0" encoding="iso-8859-1"?>
<package unique-identifier="uid" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:asd="http://www.idpf.org/asdfaf">
    <metadata>
        <dc-metadata  xmlns:dc="http://purl.org/metadata/dublin_core" xmlns:oebpackage="http://openebook.org/namespaces/oeb-package/1.0/">
            <dc:Title>Kaleidoscope: Implementing a Language with LLVM</dc:Title>
            <dc:Language>en</dc:Language>
            <dc:Creator>LLVM Community</dc:Creator>
            <dc:Copyrights>Copyright owner</dc:Copyrights>
            <dc:Publisher>Publisher</dc:Publisher>
            <!-- <x-metadata> -->
            <!--     <EmbeddedCover>images/book.jpg</EmbeddedCover> -->
            <!-- </x-metadata> -->
        </dc-metadata>
    </metadata>
    <manifest>
<!--        <item id="toc" media-type="text/x-oeb1-document" href="index.html"></item>
        <item id="content1" media-type="text/x-oeb1-document" href="LangImpl01.html"></item>
        <item id="content2" media-type="text/x-oeb1-document" href="LangImpl02.html"></item>
        <item id="content3" media-type="text/x-oeb1-document" href="LangImpl03.html"></item>
        <item id="content4" media-type="text/x-oeb1-document" href="LangImpl04.html"></item>
        <item id="content5" media-type="text/x-oeb1-document" href="LangImpl05.html"></item>
        <item id="content6" media-type="text/x-oeb1-document" href="LangImpl06.html"></item>
        <item id="content7" media-type="text/x-oeb1-document" href="LangImpl07.html"></item>
        <item id="content8" media-type="text/x-oeb1-document" href="LangImpl08.html"></item>
        <item id="content9" media-type="text/x-oeb1-document" href="LangImpl09.html"></item>
        <item id="content10" media-type="text/x-oeb1-document" href="LangImpl10.html"></item>
        <item id="ncx" media-type="application/x-dtbncx+xml" href="LangImpl.ncx"/> -->
{ITEMS}    </manifest>
    <spine toc="ncx">
<!--        <itemref idref="toc"/>
        <itemref idref="content1"/>
        <itemref idref="content2"/>
        <itemref idref="content3"/>
        <itemref idref="content4"/>
        <itemref idref="content5"/>
        <itemref idref="content6"/>
        <itemref idref="content7"/>
        <itemref idref="content8"/>
        <itemref idref="content9"/>
        <itemref idref="content10"/> -->
{ITEMREFS}    </spine>
    <guide>
        <reference type="toc" title="Table of Contents" href="{TOC_HREF}"/>
        <reference type="text" title="Book" href="{TEXT_HREF}"/>
    </guide>
</package>
"""

def BuildOPF(data, OPF_Href, TEXT_Href, TOC_Href = "index.html"):

    index_items = []
    items = []
    # ncx_items = []
    itemrefs = []
    for (name, href, id, dummy) in data:
        if href == TOC_Href:
            index_items.append(ITEM_TOC_TMPL.format(HREF=href))
            itemrefs.append(ITEMREF_TMPL.format(IDREF="toc"))
        else:
            items.append(ITEM_CONTENT_TMPL.format(N=id, HREF=href))
            itemrefs.append(ITEMREF_TMPL.format(IDREF="content{N}".format(N=id)))
    ncx_items = [ITEM_NCX_TMPL.format(HREF=OPF_Href)]
    items = index_items + items + ncx_items
    return OPF_TMPL.format(ITEMS=''.join(items), ITEMREFS=''.join(itemrefs), TOC_HREF=TOC_Href, TEXT_HREF=TEXT_Href, OPF_HREF=OPF_Href)



    pass

NAVI_TMPL = """		<navPoint id="{ID}" playOrder="{PLAY_ORDER}">
			<navLabel>
				<text>
					{TEXT}
				</text>
			</navLabel>
			<content src="{HREF}" />{SUBNAV}
		</navPoint>"""

SUBNAV_TMPL = '\n' + '\n'.join(['\t' + x for x in NAVI_TMPL.split('\n')]).replace('{SUBNAV}', '')

NCX_TMPL = """<?xml version="1.0"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
 "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
 <head>
 </head>
        <docTitle>
               <text>Book title</text>
        </docTitle>
	<navMap>
{CONTEXT}
	</navMap>
</ncx>
"""

po = 0
def BuildNCX(data):
    global po
    po = 0
    def PlayOrder():
        global po
        po += 1
        return po
    return NCX_TMPL.format(CONTEXT='\n'.join([NAVI_TMPL.format(ID="ch" + f.id, PLAY_ORDER=PlayOrder(), TEXT=f.text, HREF=f.href, SUBNAV = \
            '' + ''.join([SUBNAV_TMPL.format(ID="ch" + f.id + "_" + sf.id, PLAY_ORDER=PlayOrder(), TEXT=("%s.%s " % (f.id, ix+1)) + sf.text, HREF=sf.anchor) for ix, sf in enumerate(f.sf)]) \
            ) for f in data]))

def BuildTOC(data):
    TOC_TMPL = """<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TOC</title>
<!-- <link type="text/css" href="kf8.css" media="amzn-kf8" rel="Stylesheet"/> -->
<!-- <link type="text/css" href="mobi.css" media="amzn-mobi" rel="Stylesheet"/> -->
<!-- <link type="text/css" href="styles.css" rel="Stylesheet"/> -->
</head>
<body>

<h1 id="toc">Table of Contents</h1>

<ul>
{ITEMS}</ul>
</body>
</html>
"""
    UL_TMPL = """    <ul>
{ITEMS}    </ul>
"""
    LVL1_TMPL = """    <li><a href="{HREF}">{TEXT}</a>
{UL}    </li>
"""
    LVL2_TMPL = """        <li><a href="{HREF}">{TEXT}</a></li>
"""
    return TOC_TMPL.format(ITEMS=''.join([LVL1_TMPL.format(HREF=f.href, TEXT=f.text, UL= \
            UL_TMPL.format(ITEMS=''.join([LVL2_TMPL.format(HREF=sf.anchor, TEXT=sf.text) for sf in f.sf])) if f.sf else '' \
            ) for f in data]))
